package com.xzy.ec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xzy.ec.entity.*;
import com.xzy.ec.mapper.*;
import com.xzy.ec.service.IOmsOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-06-07
 */
@Service(value = "OrderService")
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder> implements IOmsOrderService {

    @Resource
    OmsOrderMapper mapper;
    @Resource
    GoodSpecsVOMapper goodSpecsVOMapper;
    @Resource
    PmsGoodsSpecsMapper pmsgoodsSpecsMapper;
    @Resource
    PmsGoodsMapper goodsMapper;
    @Resource
    UmsUserInfoMapper umsUserInfomapper;

    @Override
    public List<OrderVOo> getList() {
        List<OrderVOo> res=new ArrayList<>();
        try {
            for (OmsOrder o:list()){
                OmsOrder omsOrder = mapper.selectById(o.getId());
                UmsUserInfo umsUserInfo = umsUserInfomapper.selectById(omsOrder.getUserId());
                String deliveryAddr= getDeliveryAddr(o.getId());
                res.add(new OrderVOo().beComeOrderVOo(o,umsUserInfo,deliveryAddr));
            }
        }catch ( Exception e){
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public boolean updataStatus(Integer oid, Integer status) {
        try {
            System.out.println(1);
            UpdateWrapper<OmsOrder> wrapper=new UpdateWrapper<>();
            wrapper.eq("id", oid);
            wrapper.set("status", status);
            System.out.println(status);
            baseMapper.update(null,wrapper);
            System.out.println(status);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public String getDeliveryAddr(Integer oid) {
        return mapper.getDeliveryAddr(oid);
    }

    @Override
    public UserVO getUser(Integer oid) {
        return mapper.getUserVO(oid);
    }

    @Override
    public GoodSpecsVO getGoodSpecsVO(Integer oid) {
        OmsOrder omsOrder = mapper.selectById(oid);
        return goodSpecsVOMapper.getGoodSpecsVO(omsOrder.getGoodsId());
    }

    @Override
    public boolean placeOrder(Integer uid, Integer gid,Integer num) {
        try {
            if (isEnough(gid,num)){
                //库存减少
                PmsGoodsSpecs pmsGoodsSpecs = pmsgoodsSpecsMapper.selectById(gid);
                PmsGoods pmsGoods = goodsMapper.selectById(pmsGoodsSpecs.getGoodsId());
                pmsGoodsSpecs.setGoodsStock(pmsGoodsSpecs.getGoodsStock()-num);
                //下订单
                OmsOrder omsOrder = new OmsOrder();
                omsOrder.setUserId(uid);
                omsOrder.setGoodsId(gid);
                omsOrder.setGoodsCount(num);
                omsOrder.setGoodsName(pmsGoods.getName());
                omsOrder.setGoodsPrice(pmsGoodsSpecs.getGoodsPricve());
                //临时设置数据
                omsOrder.setDeliveryAddrId(1);
                omsOrder.setOrderAmountTatal(new BigDecimal(10));
                omsOrder.setOrderChannel(1);
                omsOrder.setStatus(0);
                omsOrder.setPayDate(LocalDateTime.now());
                save(omsOrder);
                pmsgoodsSpecsMapper.updateById(pmsGoodsSpecs);
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean isEnough(Integer gid, Integer num) {
        PmsGoodsSpecs pmsGoodsSpecs = pmsgoodsSpecsMapper.selectById(gid);
        if (pmsGoodsSpecs.getGoodsStock()>=num)return true;
        return false;
    }

    @Override
    public boolean updateOrder(Integer oid, OrderVO orderVO) {
        try {
            OmsOrder omsOrder = new OmsOrder();
            omsOrder.orderVOtoOmsOrder(orderVO);
            omsOrder.setId(oid);
            updateById(omsOrder);
            System.out.println(omsOrder);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteOrder(Integer oid) {
        return removeById(oid);
    }

    @Override
    public List<OrderVO> listByUid(Integer uid) {
        List<OrderVO> res=new ArrayList<>();
        try {
            QueryWrapper<OmsOrder> wrapper=new QueryWrapper<>();
            wrapper.eq("user_id", uid);
            List<OmsOrder> omsOrders = baseMapper.selectList(wrapper);
                for (OmsOrder o:list()){
                    res.add(new OrderVO().omsOrderToOrderVO(o));
                }
        }catch ( Exception e){
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public OrderVOo getOrderVO(Integer oid) {
        OrderVOo orderVOo=new OrderVOo();
        try {
            OmsOrder omsOrder = mapper.selectById(oid);
            UmsUserInfo umsUserInfo = umsUserInfomapper.selectById(omsOrder.getUserId());
            String deliveryAddr= getDeliveryAddr(oid);
            return orderVOo.beComeOrderVOo(omsOrder, umsUserInfo, deliveryAddr);
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
}
